﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeTop.复习九月份
{
    public class Solution394
    {
        public string DecodeString(string s)
        {

            Stack<string> stringS = new Stack<string>();
            Stack<int> numS = new Stack<int>();
            int preNum = 0;
            string curStr = "";
            string preStr  = "";
            for (int i = 0; i < s.Length; i++)
            {


                char c = s[i];

                if (char.IsDigit(c))
                {

                    int cur = c - '0';
                    preNum = preNum * 10 + cur;

                }
                else if (c == '[')
                {

                    numS.Push(preNum);
                    stringS.Push(curStr);
                    curStr = "";
                     preNum = 0;

                }
                else if (c == ']')
                {
                    int times = numS.Pop();
                   
                    if (stringS.Count > 0)
                    {
                        preStr = stringS.Pop();
                    }
                   
                    for (int t = 0; t < times; t++)
                    {
                        preStr += curStr;

                    }
                    curStr = preStr;
                    preStr = "";

                }
                else//字符
                {
                    curStr += c;
                }

            }

            return curStr;



        }
    }
}
